package com.xyb.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.xyb.utils.JwtUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class GlobalFilter extends ZuulFilter {
    @Resource
    private JwtUtils jwtUtils;

    /**
     * 通过返回值来设置过滤器返回类型
     * pre      路由之前调用
     * route    路由时调用
     * post     路由后调用
     * error    错误时调用
     *
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 有多个过滤器时,通过该值指定过滤器执行顺序
     * 越小越优先
     *
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 是否启用该过滤器
     * 返回 ture 表示启用
     *
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 执行单元,过滤器具体干什么事情
     * 返回任何值都会继续执行,返回 “abc” 1 null ... 都会继续执行
     * 怎么办才能终止呢?
     * com.netflix.zuul.context.RequestContext.getCurrentContext().setSendZuulResponse(false);
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String auth = request.getHeader("Authorization");


        return null;
    }
}
